package org.jypj.dev.controller;

import org.jypj.dev.dao.IWorkDao;
import org.jypj.dev.model.CodeData;
import org.jypj.dev.model.Work;
import org.jypj.dev.service.ICodeDataService;
import org.jypj.dev.service.IWorkService;
import org.jypj.dev.util.StringUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Work控制器
 *
 * @author ChenYu
 */
@Controller
@RequestMapping("workController")
public class WorkController {

    @Resource
    private IWorkService workService;

    @Resource
    private IWorkDao workDao;
    @Resource
    private ICodeDataService codeDataService;

    @RequestMapping(value = "toWorkList.action")
    public String toWorkList() {

        return "back/work/work_main";
    }

    /**
     * 跳转到工序表单页面
     *
     * @param request
     * @param id
     * @param trainType
     * @return
     */
    @RequestMapping(value = "toWorkFormUpdate.action")
    public String toWorkFormUpdate(HttpServletRequest request, String id, String trainType) {
        // 查询所有车型
        List<CodeData> codeDatas = codeDataService.selectAllByCode("cxgl");
        List<CodeData> nfcPlaces = codeDataService.selectAllByCode("wzwh");
        Work work = null;
        if (StringUtil.isNotEmpty(trainType)) {
            Map<String, Object> queryMap = new HashMap<>();
            queryMap.put("trainType", trainType);
            queryMap.put("id", id);
            work = workDao.selectWorkByIdAndTrain(queryMap);
        } else {
            work = workService.selectWorkById(id);
        }
        for (CodeData cd : codeDatas) {
            if (work.getTrainType() != null && work.getTrainType().contains(cd.getItemValue())) {
                cd.setIsCheck("true");
            } else {
                cd.setIsCheck("false");
            }
        }
        request.setAttribute("work", work);
        request.setAttribute("codeDatas", codeDatas);
        request.setAttribute("nfcPlaces", nfcPlaces);
        return "back/work/work_form_update";
    }

    /**
     * 跳转到新增界面
     *
     * @param parentId
     * @param request
     * @return
     */
    @RequestMapping(value = "toWorkFormSave.action")
    public String toWorkFormSave(String parentId, HttpServletRequest request) {
        // 查询所有车型
        List<CodeData> codeDatas = codeDataService.selectAllByCode("cxgl");
        List<CodeData> nfcPlaces = codeDataService.selectAllByCode("wzwh");

        Work w = new Work();
        // 查询并显示编号
        String newId = workService.selectMaxChildId(parentId);
        w.setId(parentId + newId);
        request.setAttribute("work", w);
        request.setAttribute("codeDatas", codeDatas);
        request.setAttribute("nfcPlaces", nfcPlaces);
        return "back/work/work_form_save";
    }

    /**
     * 查询所有数据接口
     *
     * @return code =-1 表示有异常 code=-2 表示没有数据 datas 菜单数据
     */
    @RequestMapping("selectAllWork.action")
    @ResponseBody
    public Map<String, Object> selectAllWork(String parentId, String trainType) {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("parentId", parentId);
        int code = 0;
        try {
            queryMap.put("trainType", trainType);
            List<Work> works = workService.selectAllByMap(queryMap);
            map.put("datas", works);
        } catch (Exception e) {
            code = -1;
        }
        map.put("code", code);
        return map;
    }

    /**
     * 保存工序
     *
     * @return
     */
    @RequestMapping("saveWork.action")
    @ResponseBody
    public Map<String, Object> saveWork(Work work) {
        Map<String, Object> map = new HashMap<>();
        int code = 0;
        code = workService.saveWork(work);
        map.put("code", code);
        return map;
    }

    /**
     * 更新工序
     *
     * @return
     */
    @RequestMapping("updateWork.action")
    @ResponseBody
    public Map<String, Object> updateWork(Work work) {
        Map<String, Object> map = new HashMap<>();
        int code = 0;
        code = workService.updateWork(work);
        map.put("code", code);
        return map;
    }

    /**
     * 删除操作
     *
     * @param id
     * @return
     */
    @RequestMapping("deleteWork.action")
    @ResponseBody
    public Map<String, Object> deleteWork(String id) {
        Map<String, Object> map = new HashMap<>();
        int code = 0;
        try {
            workService.deleteWorkById(id);
        } catch (Exception e) {
            code = -1;
        }
        map.put("code", code);
        return map;
    }

    /**
     * 移动节点(包括上移和下移)
     *
     * @param id1 要移动的节点id
     * @param id2 被移动的节点id
     * @return
     */
    @RequestMapping("moveWork.action")
    @ResponseBody
    public Map<String, Object> moveWork(String id1, String id2) {
        Map<String, Object> map = new HashMap<>();
        int code = 0;
        code = workService.moveWork(id1, id2);
        map.put("code", code);
        return map;
    }

}